<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="CDIO1.Models" %>
<%@ Import Namespace="CDIO1.Controllers" %>


<script runat="server">

  
    protected void gvAccount_DataBound(object sender, EventArgs e)
    {
        if (gvAccount.Rows.Count == 0)
        {
            var db = new AccountDataContext();
            Account acc = new Account();
            acc.Status = (int)Status.AVAILABLE;
            db.Accounts.InsertOnSubmit(acc);
            db.SubmitChanges();
            Response.Redirect("~/Admin/ManageAccount");
        }
        for (int i = 0; i < gvAccount.Rows.Count; i++)
        {
            Label t = gvAccount.Rows[i].FindControl("Status") as Label;
            Label t1 = gvAccount.Rows[i].FindControl("lbStatus") as Label;
            if (t.Text == "1")
            {
                t1.Text = "Available";
            }
            if (t.Text == "0")
            {
                t1.Text = "Unavailable";
            }
        }
    }

    protected void lbInsert_Click(object sender, EventArgs e)
    {
  
        var db = new AccountDataContext();
        Account acc = new Account();
        acc.Username= ((TextBox)gvAccount.FooterRow.FindControl("tbUserName")).Text;
        acc.Password = ((TextBox)gvAccount.FooterRow.FindControl("tbPassword")).Text;
        acc.Username = ((DropDownList)gvAccount.FooterRow.FindControl("ddlTypeID")).Text;
        acc.Status = (int)Status.AVAILABLE;
        db.Accounts.InsertOnSubmit(acc);
        db.SubmitChanges();
        Response.Redirect("~/Admin/ManageAccount");
    }

    protected void gvAccount_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int typeID = Int32.Parse((gvAccount.Rows[e.RowIndex].FindControl("ddlTypeID") as DropDownList).SelectedValue);
        String id = (gvAccount.Rows[e.RowIndex].FindControl("lbID") as Label).Text;
        String username = (gvAccount.Rows[e.RowIndex].FindControl("lbUsername") as Label).Text;
        
        // update Account Table
        SqlDataSource1.UpdateParameters[0].DefaultValue = typeID + "";
        SqlDataSource1.UpdateParameters[1].DefaultValue = id;
        SqlDataSource1.Update();        
        
        // configure Role
        String userRole = Roles.GetRolesForUser(username)[0];
        Roles.RemoveUserFromRole(username, userRole);
        if (typeID == 1)
        {
            // admin            
            Roles.AddUserToRole(username, "admin");            
        } else if (typeID == 2)
        {
            // prof
            Roles.AddUserToRole(username, "professor");

            // insert new professor
            string sqlConnectionStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CDIO1ConnectionString"].ConnectionString;
            string query = "";
            System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(sqlConnectionStr);
            cnn.Open();
            System.Data.SqlClient.SqlCommand cmd =
                new System.Data.SqlClient.SqlCommand("INSERT INTO [Professor] ([AccountID], [MajorID]) VALUES (@AccountID, 1)", cnn);
            cmd.Parameters.AddWithValue("@AccountID", id);
            cmd.ExecuteNonQuery();
            cnn.Close();
        } else
        {
            Roles.AddUserToRole(username, "user");            
        }
        gvAccount.EditIndex = -1;
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    ManageBloom
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h1>
        ManageAccount</h1>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="gvAccount" runat="server" AutoGenerateColumns="False" 
                CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" 
                GridLines="None" ShowFooter="True" OnDataBound="gvAccount_DataBound" 
                
                Height="173px" onrowupdating="gvAccount_RowUpdating">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID" 
                        Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="lbID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Username" SortExpression="Username">
                        <EditItemTemplate>
                            <asp:Label ID="lbUsername" runat="server" Text='<%# Bind("Username") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("Username") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Password" SortExpression="Password" 
                        Visible="False">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Password") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="User Type" SortExpression="TypeID">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlTypeID" runat="server" 
                                DataSourceID="SqlDataSourceType" DataTextField="Name" DataValueField="ID" 
                                SelectedValue='<%# Bind("TypeID") %>'>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSourceType" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>" 
                                SelectCommand="SELECT * FROM [UserType]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlTypeID" runat="server" 
                                DataSourceID="SqlDataSourceType" DataTextField="Name" DataValueField="ID" 
                                SelectedValue='<%# Bind("TypeID") %>' Enabled="False">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSourceType" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>" 
                                SelectCommand="SELECT * FROM [UserType]"></asp:SqlDataSource>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Status" SortExpression="Status" Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>' 
                                Visible="False"></asp:Label>
                            <asp:Label ID="lbStatus" runat="server"></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>' 
                                Visible="False"></asp:Label>
                            <asp:Label ID="lbStatus" runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>" 
                DeleteCommand="DELETE FROM [Account] WHERE [ID] = @ID" 
                
                InsertCommand="INSERT INTO [Account] ([Username], [Password], [TypeID], [Status]) VALUES (@Username, @Password, @TypeID, @Status)" SelectCommand="SELECT * FROM [Account]" 
                
                UpdateCommand="UPDATE [Account] SET [TypeID] = @TypeID WHERE [ID] = @ID">
                <DeleteParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="TypeID" Type="Int32" />
                    <asp:Parameter Name="ID" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="Username" Type="String" />
                    <asp:Parameter Name="Password" Type="String" />
                    <asp:Parameter Name="TypeID" Type="Int32" />
                    <asp:Parameter Name="Status" Type="Int32" />
                </InsertParameters>
            </asp:SqlDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="0" AssociatedUpdatePanelID="UpdatePanel1">
    </asp:UpdateProgress>
</asp:Content>
